package com.bnlr.lrparking.appui.mine.record;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.constant.Urls;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.OrderM;
import com.bnlr.lrparking.model.Location;
import com.bnlr.lrparking.widget.NativeDialog;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Params;
import com.bnlr.lrparking.widget.refresh.RefreshHolderUtil;
import com.bnlr.lrparking.widget.refresh.XRefreshLayout;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的停车记录
 *
 * @author Administrator
 * @date 2017/6/7
 */

public class ParkingRecordActivity extends BaseActivity {

    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.iv_nav_back)
    ImageView ivNavBack;
    @BindView(R.id.lv_mycar_list)
    RecyclerView lvMycarList;
    @BindView(R.id.rl_mycar_refresh)
    XRefreshLayout rlMycarRefresh;
    @BindView(R.id.ll_hint)
    LinearLayout llHint;
    private List<OrderM.DataBean> myCarList = new ArrayList<>();
    Double lat,lon;
    private  int currentPage = 1;
    @BindView(R.id.tv_hint_tips)
    TextView tvHint;
    @BindView(R.id.iv_hint)
    ImageView ivHint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycar);
        ButterKnife.bind(this);
        tvNavTitle.setText("停车记录");
        lat=getIntent().getDoubleExtra("lat",0.0);
        lon=getIntent().getDoubleExtra("lon",0.0);
        initView();
        tvHint.setText("暂无停车记录");
        ivHint.setImageResource(R.mipmap.norecord);
       // getData();
    }
    private MyHistoryAdapter adapter;

    private void initView() {

        linearLayoutManager = new LinearLayoutManager(baseContext);
        lvMycarList.setLayoutManager(linearLayoutManager);
        lvMycarList.setItemAnimator(new DefaultItemAnimator());
        rlMycarRefresh.setRefreshViewHolder(RefreshHolderUtil.getRefreshHolder(this));
        adapter = new MyHistoryAdapter( myCarList);
        lvMycarList.setAdapter(adapter);
        rlMycarRefresh.setDelegate(new XRefreshLayout.BGARefreshLayoutDelegate() {
            @Override
            public void onBGARefreshLayoutBeginRefreshing(XRefreshLayout refreshLayout) {
                currentPage = 1;
                getData();
            }

            @Override
            public boolean onBGARefreshLayoutBeginLoadingMore(XRefreshLayout refreshLayout) {
                return false;
            }

        });
        llHint.setVisibility(View.GONE);
        rlMycarRefresh.beginRefreshing();

        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                //  上拉加载
                currentPage++;
                getData();
            }
        },lvMycarList);
    }

    @Override
    public void getData() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetOrderByUserId");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("index", currentPage);
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<OrderM>(this, true, OrderM.class) {

                        @Override
                        public void doWork(OrderM data, String code) {
                            if (TextUtils.equals("1", code)) {
                                if (adapter != null) {
                                    if (data != null && data.getData().size() != 0) {
                                        llHint.setVisibility(View.GONE);
                                        if (currentPage == 1) {
                                            myCarList.clear();
                                            myCarList = data.getData();
                                            adapter.setNewData(data.getData());
                                        } else {
                                            myCarList.addAll(data.getData());
                                            adapter.notifyDataSetChanged();
                                        }
                                        adapter.loadMoreComplete();
                                    } else {
                                        if (currentPage == 1) {
                                            myCarList.clear();
                                            adapter.notifyDataSetChanged();
                                            llHint.setVisibility(View.VISIBLE);
                                        } else {
                                            adapter.loadMoreEnd();
                                        }
                                    }
                                }

                            } else {
                                ToastUtils.showShort(data.getMsg()+"");
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            if (rlMycarRefresh != null) {
                                rlMycarRefresh.endRefreshing();
                            }
                            if (TextUtils.equals("0", code)) {
                                if (currentPage == 1) {
                                    llHint.setVisibility(View.VISIBLE);
                                } else {
                                    adapter.loadMoreEnd();
                                }
                            }

                        }
                    }, false);

        } catch (Exception e) {

        }
    }

    public class MyHistoryAdapter extends BaseQuickAdapter<OrderM.DataBean,BaseViewHolder> {


        public MyHistoryAdapter(@Nullable List<OrderM.DataBean> data) {
            super(R.layout.item_history,data);
        }

        @Override
        protected void convert(BaseViewHolder holder, final OrderM.DataBean s) {
            holder.setText(R.id.tv_history_date,s.getBeginTime());
//            if("0".equals(s.getStatus())){
//                holder.setText(R.id.tv_history_state,"停车中");
//                holder.setTextColor(R.id.tv_history_state,getResources().getColor(R.color.red_color));
//            }else if("1".equals(s.getStatus())){
//                holder.setText(R.id.tv_history_state,"已完成");
//                holder.setTextColor(R.id.tv_history_state,getResources().getColor(R.color.gray03_color));
//            }
            holder.setText(R.id.tv_car_num,"车牌号："+s.getCarId());
            holder.setText(R.id.tv_car_order,"订单号："+s.getOrderNum());
            holder.setText(R.id.tv_car_time,"停车时间："+s.getEndTime());
            if(TextUtils.isEmpty(s.getShareValue())){
                holder.setText(R.id.tv_car_xiaohao, "消耗：0共享值");
            }else {
                holder.setText(R.id.tv_car_xiaohao, "消耗：" + s.getShareValue() + "共享值");
            }
            holder.setText(R.id.tv_carplace_name,"停车场："+s.getName());
            holder.setText(R.id.tv_address,"位置："+s.getAddress());
            RelativeLayout daohang=holder.getView(R.id.lay_daohang);
            daohang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Location loc_now = new Location(Params.CurrentLat, Params.CurrentLon, "s");
                    Location loc_end = new Location(Double.valueOf(s.getLat()), Double.valueOf(s.getLng()), "e");
                    NativeDialog msgDialog = new NativeDialog(ParkingRecordActivity.this, loc_now, loc_end);
                    msgDialog.show();
                }
            });
        }
    }
}
